﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace ConsoleApp_e1e2
{
    public class Order
    {
        public int id
        { set; get; }//订单号
        public string commodity
        { set; get; }//商品名称
        public string client
        { set; get; }//客户
        public int money
        { set; get; }//金额
        public OrderDetails detail { get; set; }
        public Order() { }
        public Order(int id, string commodity, string client, int money,OrderDetails detail)
        {
            this.id = id;
            this.commodity = commodity;
            this.client = client;
            this.money = money;
            this.detail = detail;
        }
        public override bool Equals(object obj)
        {
            if (obj is Order)
            {
                Order m = (Order)obj;
                if (m.id == id && m.commodity == commodity && m.client == client && m.money == money)
                    return m.detail.Equals(detail);
            }
            return false;
        }

        
        public override string ToString()
        {
            return Convert.ToString(id) + " " + commodity + " " + client + " " + Convert.ToString(money)+" "+detail.ToString();
        }

        public override int GetHashCode()
        {
            int hashCode = 1971021167;
            hashCode = hashCode * -1521134295 + id.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(commodity);
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(client);
            hashCode = hashCode * -1521134295 + money.GetHashCode();
            return hashCode;
        }
    }
    [Serializable]
    public class OrderDetails 
    {
        public double date { set; get; }
        public double time { set; get; }
        public int num { set; get; }//下单日期，下单时间，商品数量
        public OrderDetails() : base() { }
        public OrderDetails(double date, double time, int num) 
        {
            this.date = date;
            this.time = time;
            this.num = num;
        }

        public override bool Equals(object obj)
        {
            if (obj is OrderDetails)
            {
                OrderDetails m = (OrderDetails)obj;
                if (m.date == date && m.time == time && m.num == num)
                    return true;
            }
            return false;
        }

        public override string ToString()
        {
            return "date:"+ Convert.ToString(date) + " time:" + Convert.ToString(time) + " num:" + Convert.ToString(num);
        }

        public override int GetHashCode()
        {
            int hashCode = -1541335136;
            hashCode = hashCode * -1521134295 + date.GetHashCode();
            hashCode = hashCode * -1521134295 + time.GetHashCode();
            hashCode = hashCode * -1521134295 + num.GetHashCode();
            return hashCode;
        }
    }
    public class OrderService
    {
        //public List<Order> list { get; set; }
         public List<Order> list=new List<Order>();
        public List<Order> list1= new List<Order>();
        //public OrderService(List<OrderDetails> list)
        //{
        //    this.list = list;
        //}
        public void Export(string fileName, object obj)//xml序列化
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(List<Order>));
            FileStream fs = new FileStream(fileName, FileMode.Create);
            xmlser.Serialize(fs, obj);
            fs.Close();
        }
        public void Import(string fileName)//xml反序列化
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(List<Order>));
            FileStream fs = new FileStream(fileName, FileMode.Open);
            List<Order> orderxml = (List<Order>)xmlser.Deserialize(fs);
            list.Clear();
            foreach (Order n in orderxml)
            {
                list.Add(n);
            }
        }
        public bool add(Order order)//增加订单
        {
            foreach (var a in list)
            {
                if (a.Equals(order) == true)
                {
                    return false;
                }
            }
            list.Add(order);
            return true;
        }
        public bool search1(int id)//按商品名称查找订单
        {

            var order = from m in list where m.id == id orderby m.money select m;
            list1.Clear();
            foreach (Order n in order)
            {
                list1.Add(n);
            }
            if (list1.Count() == 0) return false;
            else return true;
        }
        //public bool search1(int id)//按订单号查找订单
        //{
        //    foreach (Order a in list)
        //    {
        //        if (a.id == id)
        //        {
        //            order1 = a;
        //            return true;
        //        }
        //    }
        //    return false;
        //    //Order[] order = (from m in list where m.id == id orderby m.money select m) as Order[];
        //    //if (order.Count() == 0)
        //    //return false;
        //    //else
        //    //{
        //    //order1 = order;
        //    //return true;
        //    //}
        //}
        //public List<Order> search1(int id)//按订单号查找订单
        //{
        //    List<Order> order = (from m in list where m.id == id orderby m.money select m)as List<Order>;
        //    return order;
        //}
        //public bool search2(string commodity)//按商品名称查找订单
        //{
        //    var order = from m in list where m.commodity == commodity orderby m.money select m;
        //    if (order.Count() == 0) return false;
        //    else
        //    {
        //        foreach (Order n in order)
        //        {
        //            Console.WriteLine(n.ToString());
        //        }
        //        return true;
        //    }
        //}
        public bool search2(string commodity)//按商品名称查找订单
        {

            var order = from m in list where m.commodity == commodity orderby m.money select m;
            list1.Clear();
            foreach(Order n in order)
            {
                list1.Add(n);
            }
            if (list1.Count() == 0) return false;
            else return true;
        }
        //public bool search3(string client)//按客户名称查找订单
        //{
        //    var order = from m in list where m.client == client orderby m.money select m;
        //    if (order.Count() == 0) return false;
        //    else
        //    {
        //        foreach (Order n in order)
        //        {

        //            Console.WriteLine(n.ToString());
        //        }
        //        return true;
        //    }
        //}
        public bool search3(string client)//按商品名称查找订单
        {
            var order = from m in list where m.client == client orderby m.money select m;
            list1.Clear();
            foreach (Order n in order)
            {
                list1.Add(n);
            }
            if (list1.Count() == 0) return false;
            else return true;
        }
        //public bool search4(int money)//按金额查找订单
        //{
        //    var order = from m in list where m.money == money orderby m.money select m;
        //    if (order.Count() == 0) return false;
        //    else
        //    {
        //        foreach (Order n in order)
        //        {
        //            Console.WriteLine(n.ToString());
        //        }
        //        return true;
        //    }
        //}
        public bool search4(int money)//按商品名称查找订单
        {

            var order = from m in list where m.money == money orderby m.money select m;
            list1.Clear();
            foreach (Order n in order)
            {
                list1.Add(n);
            }
            if (list1.Count() == 0) return false;
            else return true;
        }
        public bool remove(int id)//删除订单
        {
            bool flag = false;
            Order od = null;
            foreach (Order n in list)
            {
                if (n.id == id)
                {
                    od = n;
                    flag = true;
                }
            }
            if (flag)
                list.Remove(od);
            return flag;
        }
        public bool modify(Order order)//修改订单
        {
            foreach (Order n in list)
            {
                if (n.id == order.id)
                {
                    n.commodity = order.commodity;
                    n.client = order.client;
                    n.money = order.money;
                    n.detail=order.detail;
                    return true;
                }
            }
            return false;
        }
        public void view()//订单信息视图
        {
            foreach (Order n in list)
            {
                Console.WriteLine(n.ToString());
            }
        }
        public void sort1()//按订单号排序
        {
            var m = list.OrderBy(s => s.id);
            list = m.ToList();
        }
        public void sort2()//按订单号倒序排序
        {
            var m = list.OrderByDescending(s => s.id);
            list = m.ToList();
        }
        public void sort3()//按金额排序
        {
            var m = list.OrderBy(s => s.money);
            list = m.ToList();
        }
        public void sort4()//按金额倒序排序
        {
            var m = list.OrderByDescending(s => s.money);
            list = m.ToList();
        }
        public void sort5()//按日期倒序排序
        {
            var m = list.OrderByDescending(s => s.detail.date);
            list = m.ToList();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //        OrderService service = new OrderService();
            //        OrderDetails order1 = new OrderDetails(1001, "book", "teacher", 65, 3.28, 11.30, 25);
            //        OrderDetails order2 = new OrderDetails(1002, "pen", "student", 10, 1.28, 18.30, 1);
            //        OrderDetails order3 = new OrderDetails(1003, "phone", "Jack", 3900, 3.2, 15.23, 1);
            //        OrderDetails order4 = new OrderDetails(1004, "tea", "Jane", 245, 1.11, 11.39, 2);
            //        OrderDetails order5 = new OrderDetails(1005, "book", "student", 70, 3.29, 8.22, 1);
            //        OrderDetails order6 = new OrderDetails(1001, "book", "teacher", 60, 3.29, 8.22, 30);
            //        service.add(order1);
            //        service.add(order2);
            //        service.add(order1);
            //        service.add(order3);
            //        service.add(order4);
            //        service.add(order5);
            //        service.view();
            //        Console.WriteLine();
            //        service.search1(1003);
            //        Console.WriteLine();
            //        service.search2("book");
            //        Console.WriteLine();
            //        service.search3("student");
            //        Console.WriteLine();
            //        service.search4(245);
            //        Console.WriteLine();
            //        service.remove(order4);
            //        service.modify(order6);
            //        service.sort1();
            //        Console.WriteLine();
            //        service.view();
            //        service.sort2();
            //        Console.WriteLine();
            //        service.view();
            //        service.sort3();
            //        Console.WriteLine();
            //        service.view();
            //        service.sort4();
            //        Console.WriteLine();
            //        service.view();
            //        XmlSerializer xmlser = new XmlSerializer(typeof(List<OrderDetails>));
            //        string xmlFileName = "s.xml";
            //        service.Export(xmlser, xmlFileName, service.list);
            //        Console.WriteLine();
            //        service.Import(xmlser, xmlFileName);
        }
    }
}

